home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 2000 November: Tool Chest / Dev.CD Nov 00 TC Disk 2.toast / pc / sample code / printing / isimagewriter / isimagewriter.c < prev    next >
Encoding:
C/C++ Source or Header  |  2000-09-28  |  5.3 KB  |  190 lines

  1. /*
  2.     File:        IsImageWriter.c
  3.  
  4.     Contains:    This sample program demonstrates how to determine:
  5.                 1. If an ImageWriter is connected
  6.                 2. What type of ImageWriter
  7.                 3. Which port it is connected to (invalid if AppleTalk IW)
  8.  
  9.     Written by: Brigham Stevens    
  10.  
  11.     Copyright:    Copyright © 1992-1999 by Apple Computer, Inc., All Rights Reserved.
  12.  
  13.                 You may incorporate this Apple sample source code into your program(s) without
  14.                 restriction. This Apple sample source code has been provided "AS IS" and the
  15.                 responsibility for its operation is yours. You are not permitted to redistribute
  16.                 this Apple sample source code as "Apple sample source code" after having made
  17.                 changes. If you're going to re-distribute the source, we require that you make
  18.                 it clear in the source that the code was descended from Apple sample source
  19.                 code, but that you've made changes.
  20.  
  21.     Change History (most recent first):
  22.                 7/26/1999    Karl Groethe    Updated for Metrowerks Codewarror Pro 2.1
  23.                 
  24.  
  25. */
  26. #include <Printing.h>
  27. #include <Resources.h>
  28. #include <Memory.h>
  29. #include <Dialogs.h>
  30. #include <OSUtils.h>
  31. #include <OSEvents.h>
  32. #include <Fonts.h>
  33. #include <Menus.h>
  34. #include <Serial.h>
  35. #include <stdio.h>
  36. #include <OSUtils.h>
  37.  
  38.  
  39. /* these are the wDev IDs for the ImageWriter and IW LQ printers */
  40. /* see the Q & A Stack for a discussion of the wDev field. */
  41. #define IMAGE_WRITER_WDEVID 1
  42. #define IMAGE_WRITER_LQ_WDEVID 5
  43.  
  44.  
  45. /*  
  46.     This is returned from the IsImageWriter function to
  47.     indicate which kind of ImageWriter is returned.
  48.     a result of zero, or NotImageWriter means that the
  49.     printer is - not an ImageWriter.
  50.     
  51. */
  52. typedef enum { 
  53.     NotImageWriter, 
  54.     SerialImageWriter, 
  55.     AppleTalkImageWriter, 
  56.     SerialImageWriterLQ, 
  57.     AppleTalkImageWriterLQ 
  58. } ImWrtKindFlag;
  59.  
  60. ImWrtKindFlag IsImageWriter();
  61. SPortSel GetPrinterConnection();
  62.  
  63. ImWrtKindFlag IsImageWriter()
  64. /*
  65.     This function determines if we are talking to an ImageWriter and
  66.     then if so, what kind.  This function is reliant upon the signature
  67.     resource being 'IWRX' for an AppleTalk ImageWriter, or 'IWRT' for a 
  68.     serial connected ImageWriter, 
  69.     or respectivly 'BWRT', or 'BWRX' for an LQ ImageWriter
  70. */
  71. {
  72.     THPrint    tpr;
  73.     short    prtNum;
  74.     OSErr    myErr;
  75.     ImWrtKindFlag     result = NotImageWriter;
  76.     
  77.     tpr = (THPrint)NewHandle(sizeof(TPrint));
  78.     if(!tpr) {
  79.         myErr = MemError();
  80.         goto bail;
  81.     }
  82.     
  83.     PrOpen();        /* open the print driver */
  84.     if(myErr = PrError())    goto bail;
  85.     
  86.     PrintDefault(tpr);    /* init the print record */
  87.     if(myErr = PrError())    goto bail;
  88.  
  89.     /* get the printer ID number from the high byte */
  90.     /* of the wDev field.  This is a quasi-unique */
  91.     /* number for each printer type. See the Q & A Stack */
  92.     /* for more information on the wDev field, and the */
  93.     /* problems with relying on it. For the purposed of checking */
  94.     /* for a color ribbon, you should be ok.  You may have to */
  95.     /* revise this technique at some point, when the new print architecture */
  96.     /* is available. */
  97.     prtNum = (**tpr).prStl.wDev >> 8;
  98.  
  99.     if ( prtNum == IMAGE_WRITER_WDEVID )  {
  100.         /* we are talking to an ImageWriter, now see
  101.             if it is serial or appleTalk. */
  102.         if(Count1Resources('IWRT') > 0)
  103.             result = SerialImageWriter;
  104.         else if(Count1Resources('IWRX') > 0)
  105.             result = AppleTalkImageWriter;
  106.         else /* This must be a printer acting like an ImageWriter */
  107.             result = NotImageWriter;
  108.     }
  109.     else if ( prtNum == IMAGE_WRITER_LQ_WDEVID ) {
  110.         if(Count1Resources('BWRT') > 0)
  111.             result = SerialImageWriterLQ;
  112.         else if(Count1Resources('BWRX') > 0)
  113.             result = AppleTalkImageWriterLQ;
  114.         else /* This must be a printer acting like an ImageWriterLQ */
  115.             result = NotImageWriter;
  116.     }
  117. bail:
  118.     if(tpr)
  119.         DisposeHandle((Handle)tpr);    /* get rid of our print handle */
  120.     PrClose();        /* close the print driver */
  121.     return result;
  122. }
  123.  
  124. SPortSel GetPrinterConnection()
  125. /*
  126.     This function returns sPortA or sPortB, whichever
  127.     the printer is connected to, as determined from
  128.     parameter RAM.  Note:  the result is only valid in
  129.     the case where you have a printer that is connected
  130.     directly to the Macintosh.
  131. */
  132. {
  133.     SysPPtr    pRAMptr;     /* See IM-II pg. 370 */ 
  134.  
  135.     pRAMptr = GetSysPPtr();  /* See IM-II pg. 381 */  
  136.  
  137.             /* bit zero indicates which port */
  138.             /* bit zero = 0 = printer port */
  139.             /* bit zero = 1 = modem port */
  140.     if(pRAMptr->kbdPrint & 0x0001)
  141.         return sPortA;        /* Modem Port */
  142.     else
  143.         return sPortB;        /* Printer Port */
  144. }
  145.  
  146.  
  147. void main ()
  148. {
  149.  
  150.     ImWrtKindFlag        iw;
  151.     SPortSel            whichPort;
  152.     
  153.     InitGraf(&qd.thePort);
  154.     InitFonts();
  155.     InitWindows();
  156.     InitMenus();
  157.     InitCursor();
  158.     TEInit();
  159.     FlushEvents(everyEvent, 0);
  160.     InitDialogs(nil);
  161.  
  162.     printf("\n");
  163.     switch(iw = IsImageWriter()) {
  164.         case NotImageWriter:
  165.                 printf("\nWe are NOT connected to an ImageWriter.");
  166.                 break;
  167.         case SerialImageWriter:
  168.                 printf("\nWe are connected to a Serial ImageWriter.");
  169.                 break;
  170.         case AppleTalkImageWriter:
  171.                 printf("\nWe are connected to an AppleTalk ImageWriter.");
  172.                 break;
  173.         case SerialImageWriterLQ:
  174.                 printf("\nWe are connected to a Serial ImageWriterLQ.");
  175.                 break;
  176.         case AppleTalkImageWriterLQ:
  177.                 printf("\nWe are connected to a AppleTalk ImageWriterLQ.");
  178.                 break;
  179.         default:
  180.                 printf("\nIsImageWriter error code: %i",iw);
  181.     }
  182.     whichPort = GetPrinterConnection();
  183.     if(whichPort == sPortA)
  184.         printf("\nThe printer is connected to the modem port.");
  185.     else if(whichPort == sPortB)
  186.         printf("\nThe printer is connected to the printer port.");
  187.     else
  188.         printf("\nThe printer is connected to an unknown port.");
  189.     printf("\n");
  190. }